﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SensorNetworkUI;
namespace WebAccess
{
    public partial class UserNetwork : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Page.Title = CheckBoxList1.Items.GetType().ToString();

        }

        protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

            UserDataStructure refe = Global.users.Lookup(DropDownList1.SelectedItem.Text);
            refe.GetNetworks();
            CheckBoxList1.ClearSelection();
            foreach (NetworkDataStructure net in refe.AllowedNetworks)
            {

                ListItem it = CheckBoxList1.Items.FindByValue(net.Id.ToString());

                it.Selected = true;
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            UserDataStructure refe = Global.users.Lookup(DropDownList1.SelectedItem.Text);
            refe.AllowedNetworks = new List<NetworkDataStructure>();
            foreach (ListItem it in CheckBoxList1.Items)
            {
                if (it.Selected)
                {
                    NetworkDataStructure net = new NetworkDataStructure();
                    net.Id = int.Parse(it.Value);
                    refe.AllowedNetworks.Add(net);
                    
                }
            }
            
            refe.UpdateNetworkList();
            refe.PopulateNetworks();
            Session["Username"] = refe;
        }
    }
}